package cn.caixiong.org.controller;

import cn.caixiong.basic.utils.JsonResult;
import cn.caixiong.basic.utils.PageList;
import cn.caixiong.org.domain.Employee;
import cn.caixiong.org.query.BaseQuery;
import cn.caixiong.org.service.IEmployeeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    //注入依赖
    @Autowired
    private IEmployeeService Service;

    //查询所有
    @GetMapping
    public JsonResult findAll() {
        List<Employee> list = Service.findAll();
        return JsonResult.createSuccess(list);
    }
    //分页
    @PostMapping("/findPage")
    public PageList<Employee> findPage(@RequestBody BaseQuery query) {
        return Service.findPage(query);
    }

    //查询单个
    @GetMapping("/findOne/{id}")
    public JsonResult findOne(@PathVariable("id") Long id) {
        Service.findOne(id);
        return JsonResult.createSuccess();
    }

    //新增或修改
    @PostMapping
    @ApiOperation(value="新增/修改用户", notes="描述")
    //新增与修改要添加跳转的 主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)
    public JsonResult save(@ApiParam(value = "新增/修改用户参数", required = true)@RequestBody Employee department) {
        try {
            Service.saveOrUpdata(department);
            return JsonResult.createSuccess();
        } catch (Exception e) {
           e.printStackTrace();
            return JsonResult.createError("系统异常,请联系管理员");
        }

    }

    //删除
    @DeleteMapping("/{id}")
    public JsonResult deleteId(@PathVariable("id") Long id) {
        try {
            Service.deleteId(id);
            return JsonResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.createError("系统异常,请联系管理员");
        }
    }

    //查询经理们
    @GetMapping("/managerAll")
    public JsonResult managerAll() {
        List<Employee> list = Service.managerAll();
        return JsonResult.createSuccess(list);
    }


}
